package com.sgb.kjwl.viewmodel.self.userInfo;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.BankInfoEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.userInfo.BankInfoActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BankInfoModel extends BaseViewModel {
    private BankInfoActivity activity;
    private SelfRepertory repertory;

    public BankInfoModel(BankInfoActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void saveModel(String papertitle, String dueNumber, String address, String mobile, String bank, String bankAccount) {
        Map<String, String> map = new HashMap<>();
        map.put("title", papertitle);
        map.put("taxNo", dueNumber);
        map.put("address", address);
        map.put("mobile", mobile);
        map.put("bank", bank);
        map.put("bankAccount", bankAccount);
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().commitBankInfo(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.success();
                }else {
                    MToast.showToast(activity,response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getBankInfo(){
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getBankInfo().enqueue(new Callback<BaseEntity<BankInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<BankInfoEntity>> call, Response<BaseEntity<BankInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showBankInfo(response.body().getData());
                }else {
                    MToast.showToast(activity,response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<BankInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
